package com.qzsoft.zhjd.materiel.modules.materieldevice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.system.modules.zhjdsysdept.entity.ZhjdSysDeptEntity;
import com.qzsoft.system.modules.zhjdsysdept.service.ZhjdSysDeptService;
import com.qzsoft.zhjd.common.annotation.ZhjdDataFilter;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.enums.CodeRuleEnmus;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;
import com.qzsoft.zhjd.materiel.modules.materieldevice.entity.MaterielDeviceEntity;
import com.qzsoft.zhjd.materiel.modules.materieldevice.mapper.MaterielDeviceMapper;
import com.qzsoft.zhjd.materiel.modules.materieldevice.service.MaterielDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author linhuawei
 * @ClassName MaterielDeviceServiceImpl
 * @Description 地磅设备业务接口实现
 * @since 2022-03-31
 */
@Service
public class MaterielDeviceServiceImpl extends ServiceImpl<MaterielDeviceMapper, MaterielDeviceEntity>
        implements MaterielDeviceService {

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Autowired
    private ZhjdSysDeptService zhjdSysDeptService;

    @Override
    @ZhjdDataFilter
    public IPage<MaterielDeviceEntity> page(Map<String, Object> params) {
        IPage<MaterielDeviceEntity> page = baseServiceComponent.getPage(params, MaterielDeviceEntity.class);
        QueryWrapper<MaterielDeviceEntity> queryWrapper = new QueryWrapper<MaterielDeviceEntity>();
        if (ObjectUtils.isNotEmpty(params.get("baseCode"))) {
            queryWrapper.eq("base_code", params.get("baseCode").toString());
        }
        if (ObjectUtils.isNotEmpty(params.get("name"))) {
            queryWrapper.like("name", params.get("name").toString());
        }
        if (ObjectUtils.isNotEmpty(params.get(ZhjdDataFilter.DATA_FILTER_SQL))) {
            queryWrapper.apply(params.get(ZhjdDataFilter.DATA_FILTER_SQL).toString());
        }
        IPage<MaterielDeviceEntity> pageData = baseMapper.selectPage(page, queryWrapper);

        List<ZhjdSysDeptEntity> baseList = zhjdSysDeptService.listForBase(new HashMap<>());
        Map<String, String> baseMap = new HashMap<>();
        baseList.forEach(base -> {
            baseMap.put(base.getCode(), base.getName());
        });
        for (MaterielDeviceEntity device : pageData.getRecords()) {
            device.setBaseName(baseMap.getOrDefault(device.getBaseCode(), ""));
        }
        return pageData;
    }

    @Override
    @ZhjdDataFilter
    public List<MaterielDeviceEntity> list(Map<String, Object> params) {
        QueryWrapper<MaterielDeviceEntity> queryWrapper = new QueryWrapper<MaterielDeviceEntity>();
        if (ObjectUtils.isNotEmpty(params.get("baseCode"))) {
            queryWrapper.eq("base_code", params.get("baseCode").toString());
        }
        if (ObjectUtils.isNotEmpty(params.get("name"))) {
            queryWrapper.like("name", params.get("name").toString());
        }
        if (ObjectUtils.isNotEmpty(params.get(ZhjdDataFilter.DATA_FILTER_SQL))) {
            queryWrapper.apply(params.get(ZhjdDataFilter.DATA_FILTER_SQL).toString());
        }
        queryWrapper.orderByAsc("base_code,name");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public MaterielDeviceEntity getByCode(String code) {
        QueryWrapper<MaterielDeviceEntity> queryWrapper = new QueryWrapper<MaterielDeviceEntity>();
        queryWrapper.eq("code", code);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean save(MaterielDeviceEntity entity) {
        // 判断基地是否存在
        String baseCode = entity.getBaseCode();
        ZhjdSysDeptEntity base = zhjdSysDeptService.getBaseByCode(baseCode);
        if (null == base) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "所属工地不存在");
        }

        // 判断编号是否重复
        QueryWrapper<MaterielDeviceEntity> queryWrapper = new QueryWrapper<MaterielDeviceEntity>();
        queryWrapper.eq("code",  base.getCode());
        Integer count = baseMapper.selectCount(queryWrapper);
        if (null != count && 0 < count) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "设备编号[" + base.getCode() + "]已经存在");
        }
        return super.save(entity);
    }

}
